import { Injectable } from '@angular/core';
import { ipcRenderer, webFrame, remote, BrowserWindow } from 'electron';
import * as childProcess from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
import * as os from 'os';

@Injectable({
  providedIn: 'root'
})
export class ElectronService {
  app: Electron.App;
  shell: Electron.Shell;
  ipcRenderer: typeof ipcRenderer;
  webFrame: typeof webFrame;
  remote: typeof remote;
  BrowserWindow: typeof BrowserWindow;
  screen: Electron.Screen;
  childProcess: typeof childProcess;
  fs: typeof fs;
  os: typeof os;
  path: typeof path;

  private log: any;

  get isElectron(): boolean {
    return !!(window && window.process && window.process.type);
  }

  constructor() {
    // Conditional imports
    if (this.isElectron) {
      this.ipcRenderer = window.require('electron').ipcRenderer;
      this.shell = window.require('electron').shell;
      this.webFrame = window.require('electron').webFrame;
      this.remote = window.require('electron').remote;
      this.BrowserWindow = this.remote.BrowserWindow;
      this.screen = this.remote.screen;
      this.app = this.remote.app;
      this.childProcess = window.require('child_process');
      this.fs = window.require('fs');
      this.os = window.require('os');
      this.path = window.require('path');
    }
  }
  private win: BrowserWindow = null;
  public isWindowMax = false;
  getLocalPort() {
    if (!this.win) this.win = this.BrowserWindow.getFocusedWindow();
    return (this.win as any).localPort;
  }
  getIPAddress() {
    var interfaces = this.os.networkInterfaces();
    for (var devName in interfaces) {
      var iface = interfaces[devName];
      for (var i = 0; i < iface.length; i++) {
        var alias = iface[i];
        if (alias.family == 'IPv4' && alias.address !== '127.0.0.1' && !alias.internal) {
          return alias.address;
        }
      }
    }
    return '127.0.0.1';
  }
  /*isWindowMax(){
    if (!this.win)this.win = this.BrowserWindow.getFocusedWindow();
    if (!this.win)return false;
    return this.win.isMaximized();
  }*/
  closeConfirm(e) {
    e.preventDefault();
    this.win.webContents.send('win-close-confirm');
  }
  windowOper(oper: string) {
    if (!this.win) this.win = this.BrowserWindow.getFocusedWindow();
    if (!this.win) return false;
    switch (oper) {
      case 'window-min':
        this.win.minimize();
        break;
      case 'window-max':
        if (this.win.isMaximized()) {
          this.win.unmaximize();
        } else {
          this.win.maximize();
        }
        this.isWindowMax = this.win.isMaximized();
        break;
      case 'window-close':
        //this.win.close();
        //this.app.quit();
        this.ipcRenderer.send('window-close', false);
        break;
      case 'window-open-devtool':
        this.win.webContents.openDevTools();
        break;
      case 'window-move-start':
        this.window_move_start();
        break;
      case 'window-move-end':
        this.window_move_end();
        break;
    }
  }
  private movingInterval = null;
  window_move_start() {
    if (this.win.isMaximized()) {
      this.win.unmaximize()
    }
    const winPosition = this.win.getPosition();
    let winStartPosition = { x: winPosition[0], y: winPosition[1] };
    let mouseStartPosition = this.screen.getCursorScreenPoint();
    if (this.movingInterval) {
      clearInterval(this.movingInterval);
    }
    this.movingInterval = setInterval(() => {
      const cursorPosition = this.screen.getCursorScreenPoint();
      const x = winStartPosition.x + cursorPosition.x - mouseStartPosition.x;
      const y = winStartPosition.y + cursorPosition.y - mouseStartPosition.y;
      this.win.setPosition(x, y, true);
    }, 20);
  }
  window_move_end() {
    clearInterval(this.movingInterval);
    this.movingInterval = null;
  }
}
